package com.tony.business.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @author x
 * @date 2019/8/16 10:57
 */
@Configuration
public class DateConfig {

    /**
     * Month格式化字符串
     */
    private static final String MONTH_FORMAT = "yyyy-MM";
    /**
     * Date格式化字符串
     */
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * DateTime格式化字符串
     */
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * Time格式化字符串
     */
    private static final String TIME_FORMAT = "HH:mm:ss";

    @Bean
    public Formatter<Date> dateFormatter() {
        return new Formatter<Date>() {
            SimpleDateFormat sdfDateTime = new SimpleDateFormat(DATETIME_FORMAT);
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT);
            SimpleDateFormat sdfMonth = new SimpleDateFormat(MONTH_FORMAT);
            SimpleDateFormat sdfTime = new SimpleDateFormat(TIME_FORMAT);
            @Override
            public Date parse(String text, Locale locale) {
                if(StringUtils.isEmpty(text)){
                    return null;
                }
                Date date = null;
                try {
                    if (text.length() == DATE_FORMAT.length()) {
                        date = sdfDate.parse(text);
                    } else if (text.length() == MONTH_FORMAT.length()) {
                        date = sdfMonth.parse(text);
                    } else if (text.length() == TIME_FORMAT.length()) {
                        date = sdfTime.parse(text);
                    }else{
                        date = sdfDateTime.parse(text);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return date;
            }

            @Override
            public String print(Date date, Locale locale) {
                return sdfDateTime.format(date);
            }
        };
    }

    @Bean
    public Formatter<LocalDate> localDateFormatter() {
        return new Formatter<LocalDate>() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
            @Override
            public LocalDate parse(String text, Locale locale) {
                if(StringUtils.isEmpty(text)){
                    return null;
                }
                return LocalDate.parse(text, formatter);
            }

            @Override
            public String print(LocalDate localDate, Locale locale) {
                return formatter.format(localDate);
            }
        };
    }

    @Bean
    public Formatter<LocalTime> localTimeFormatter(){
        return new Formatter<LocalTime>() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
            @Override
            public LocalTime parse(String text, Locale locale) {
                if(StringUtils.isEmpty(text)){
                    return null;
                }
                return LocalTime.parse(text);
            }

            @Override
            public String print(LocalTime localTime, Locale locale) {
                return formatter.format(localTime);
            }
        };
    }

    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter() {
        return new Formatter<LocalDateTime>() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
            @Override
            public LocalDateTime parse(String text, Locale locale) {
                if(StringUtils.isEmpty(text)){
                    return null;
                }
                return LocalDateTime.parse(text, formatter);
            }

            @Override
            public String print(LocalDateTime localDateTime, Locale locale) {
                return formatter.format(localDateTime);
            }
        };
    }

}
